<?php
/**
 * FormLoader.php
 *
 * @category 	Madoqua
 * @package 	Controller
 * @package 	Helper
 */

/** base class */
require_once 'Zend/Controller/Action/Helper/Abstract.php';

/** Zend_Loader */
require_once 'Zend/Loader.php';

class Madoqua_Controller_Action_Helper_FormLoader extends Zend_Controller_Action_Helper_Abstract
{
    /**
     * path spec
     *
     * @var string
     */
    protected $_formPathSpec = ':moduleDir/forms';

    /**
     * set path spec
     *
     * @param string $spec
     * @return Madoqua_Controller_Action_Helper_FormLoader
     */
    public function setFormPathSpec($spec)
    {
        $this->_formPathSpec = $spec;

        return $this;
    }

    /**
     * load()
     *
     * @param string|array $forms
     * @return void
     * @todo make spotty turn this into an inflector based approach
     * @todo move to Madoqua_Loader
     */
    public function load($forms)
    {
        $forms = (array) $forms;
        //in array form

        $frontController = Madoqua_Application::getFront();
        //the front controller

        $modules = Madoqua_Application::getModules();
        //all modules currently in the application

        $moduleDirs = array();
        foreach($frontController->getControllerDirectory() as $name => $dir) {
            $moduleDirs[$name] = realpath($dir . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);
            //translate into the real path (which is one dir above the controller dir)
        }
        //an array of module name => module directory

        foreach ($forms as $form) {
            $split = split('_', $form);
            //form split
            foreach($split as $key => $item) {
                $split[$key] = ucfirst($item);
            }
            //normalize split array to upper case

            $form = implode('_', $split);
            //normalized form name
            
            if (class_exists($form)) {
                continue;
            }
            //if the class exists, do nothing (go to next)

            $requestModule = array_shift($split);
            $requestModule[0] = strtolower($requestModule[0]);
            //requested module (normalized)

            if (!in_array($requestModule, $modules)) {
                //the request module doesn't exist
                throw new Exception('Module ' . $requestModule . ' does not exist, can\'t load form ' . $form);
            }

            $path = str_replace(':moduleDir', $moduleDirs[$requestModule] , $this->_formPathSpec) . DIRECTORY_SEPARATOR;
            //the path inside the module dir we'll be looking for the form


            $file = array_pop($split) . '.php';
            //the file we're going to load

            $subdir = implode(DIRECTORY_SEPARATOR, $split);
            //the sub dir is what's left of $split

            $path = $path . $subdir . DIRECTORY_SEPARATOR;
            
            Zend_Loader::loadFile($file, $path);

            if (!class_exists($form)) {
                throw new Zend_Controller_Action_Exception($form . ' not found, file: ' . $path . $file);
            }
            //check if we loaded the form right
        }
    }

    /**
     * load a form and get it
     *
     * @param string $form
     * @param null|array
     * @return Madoqua_Form
     */
    public function get($form = null, $arguments = array()) {
        if ($form == null) {
            $form = $this->getRequest()->getModuleName() . '_';
            $form .= $this->getRequest()->getControllerName() . '_';
            $form .= $this->getRequest()->getActionName();
        }
        //if form isn't set try to find one matching the current request

        $this->load($form);
        //load the form

        return new $form($arguments);
    }

    /**
     * direct method
     *
     * @param string|array $form
     * @return void
     */
    public function direct($form = null)
    {
        return $this->get($model);
    }

}
